home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg1.cab / prefcalls.js < prev    next >
Text File  |  2005-07-13  |  7KB  |  231 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  * Mitesh Shah <mitesh@netscape.com>
  24.  * Brian Nesse <bnesse@netscape.com>
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the NPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the NPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. const nsILDAPURL = Components.interfaces.nsILDAPURL;
  41. const LDAPURLContractID = "@mozilla.org/network/ldap-url;1";
  42. const nsILDAPSyncQuery = Components.interfaces.nsILDAPSyncQuery;
  43. const LDAPSyncQueryContractID = "@mozilla.org/ldapsyncquery;1";
  44. const nsIPrefService = Components.interfaces.nsIPrefService;
  45. const PrefServiceContractID = "@mozilla.org/preferences-service;1";
  46.  
  47. // set on a platform specific basis in platform.js
  48. platform = { value: "" };
  49.  
  50. var gVersion;
  51.  
  52. function getPrefBranch() {
  53.     
  54.     var prefService = Components.classes[PrefServiceContractID]
  55.                                 .getService(nsIPrefService);    
  56.     return prefService.getBranch(null);
  57. }
  58.  
  59. function pref(prefName, value) {
  60.  
  61.     try { 
  62.         var prefBranch = getPrefBranch();
  63.  
  64.         if (typeof value == "string") {
  65.             prefBranch.setCharPref(prefName, value);
  66.         }
  67.         else if (typeof value == "number") {
  68.             prefBranch.setIntPref(prefName, value);
  69.         }
  70.         else if (typeof value == "boolean") {
  71.             prefBranch.setBoolPref(prefName, value);
  72.         }
  73.     }
  74.     catch(e) {
  75.         displayError("pref", e);
  76.     }
  77. }
  78.  
  79. function defaultPref(prefName, value) {
  80.     
  81.     try {
  82.         var prefService = Components.classes[PrefServiceContractID]
  83.                                     .getService(nsIPrefService);        
  84.         var prefBranch = prefService.getDefaultBranch(null);
  85.         if (typeof value == "string") {
  86.             prefBranch.setCharPref(prefName, value);
  87.         }
  88.         else if (typeof value == "number") {
  89.             prefBranch.setIntPref(prefName, value);
  90.         }
  91.         else if (typeof value == "boolean") {
  92.             prefBranch.setBoolPref(prefName, value);
  93.         }
  94.     }
  95.     catch(e) {
  96.         displayError("defaultPref", e);
  97.     }
  98. }
  99.  
  100. function lockPref(prefName, value) {
  101.     
  102.     try {
  103.         var prefBranch = getPrefBranch();
  104.         
  105.         if (prefBranch.prefIsLocked(prefName))
  106.             prefBranch.unlockPref(prefName);
  107.         
  108.         defaultPref(prefName, value);
  109.         
  110.         prefBranch.lockPref(prefName);
  111.     }
  112.     catch(e) {
  113.         displayError("lockPref", e);
  114.     }
  115. }
  116.  
  117. function unlockPref(prefName) {
  118.  
  119.     try {
  120.  
  121.         var prefBranch = getPrefBranch();
  122.         prefBranch.unlockPref(prefName);
  123.     }
  124.     catch(e) {
  125.         displayError("unlockPref", e);
  126.     }
  127. }
  128.  
  129. function getPref(prefName) {
  130.     
  131.     try {
  132.         var prefBranch = getPrefBranch();
  133.         
  134.         switch (prefBranch.getPrefType(prefName)) {
  135.             
  136.         case prefBranch.PREF_STRING:
  137.             return prefBranch.getCharPref(prefName);
  138.             
  139.         case prefBranch.PREF_INT:
  140.             return prefBranch.getIntPref(prefName);
  141.             
  142.         case prefBranch.PREF_BOOL:
  143.             return prefBranch.getBoolPref(prefName);
  144.         default:
  145.             return null;
  146.         }
  147.     }
  148.     catch(e) {
  149.         displayError("getPref", e);
  150.     }
  151. }
  152.  
  153.  
  154. function setLDAPVersion(version) {
  155.     gVersion = version;
  156. }
  157.  
  158.  
  159. function getLDAPAttributes(host, base, filter, attribs) {
  160.     
  161.     try {
  162.         var url = Components.classes[LDAPURLContractID].createInstance(nsILDAPURL);
  163.     
  164.         url.spec = "ldap://" + host + "/" + base + "?" + attribs 
  165.                    + "?sub?" +  filter;
  166.         var ldapquery = Components.classes[LDAPSyncQueryContractID]
  167.                                   .createInstance(nsILDAPSyncQuery);
  168.         // default to LDAP v3
  169.         if (!gVersion)
  170.           gVersion = Components.interfaces.nsILDAPConnection.VERSION3
  171.     // user supplied method
  172.         processLDAPValues(ldapquery.getQueryResults(url, gVersion));
  173.     }
  174.     catch(e) {
  175.         displayError("getLDAPAttibutes", e);
  176.     }
  177. }
  178.  
  179. function getLDAPValue(str, key) {
  180.  
  181.     try {
  182.         if (str == null || key == null)
  183.             return null;
  184.         
  185.         var search_key = "\n" + key + "=";
  186.         
  187.         var start_pos = str.indexOf(search_key);
  188.         if (start_pos == -1)
  189.             return null;
  190.         
  191.         start_pos += search_key.length;
  192.         
  193.         var end_pos = str.indexOf("\n", start_pos);
  194.         if (end_pos == -1)
  195.             end_pos = str.length;
  196.         
  197.         return str.substring(start_pos, end_pos);
  198.     }
  199.     catch(e) {
  200.         displayError("getLDAPValue", e);
  201.     }
  202. }
  203.  
  204. function displayError(funcname, message) {
  205.  
  206.     try {
  207.         var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  208.                                       .getService(Components.interfaces.nsIPromptService);
  209.         var bundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
  210.                                .getService(Components.interfaces.nsIStringBundleService)
  211.                                .createBundle("chrome://autoconfig/locale/autoconfig.properties");
  212.  
  213.          var title = bundle.GetStringFromName("autoConfigTitle");
  214.          var msg = bundle.formatStringFromName("autoConfigMsg", [funcname], 1);
  215.          promptService.alert(null, title, msg + " " + message);
  216.     }
  217.     catch(e) { }
  218. }
  219.  
  220. function getenv(name) {
  221.     try {
  222.         var environment = Components.classes["@mozilla.org/process/environment;1"].
  223.             getService(Components.interfaces.nsIEnvironment);
  224.         return environment.get(name);
  225.     }
  226.     catch(e) {
  227.         displayError("getEnvironment", e);
  228.     }
  229. }
  230.  
  231.